//
//  Helpers.m
//  insyte
//
//  Created by Vu Dang Tuan on 6/18/15.
//  Copyright (c) 2015 Robert. All rights reserved.
//

#import "Helpers.h"
#import "UIColor+InsyteColors.h"
#import "Constants.h"
#import "KeychainItemWrapper.h"


@implementation Helpers
+ (void)StyleViewWithBorderColor:(UIView *)view color:(UIColor *)color corner:(float)corner
{
    view.layer.borderColor = color.CGColor;
    view.layer.borderWidth = 1.5f;
    view.layer.cornerRadius = corner;
}

+ (void)StyleButtonWithBorderColor:(UIButton *)btn color:(UIColor *)color corner:(float)corner
{
    btn.layer.borderColor = color.CGColor;
    btn.layer.borderWidth = 1.5f;
    btn.layer.cornerRadius = corner;
}
+ (void)scrollView:(UIView *)view y:(float)y{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3f];
    CGRect frame = view.frame;
    frame.origin.y = y;
    view.frame = frame;
    [UIView commitAnimations];
}

// validate email
+ (BOOL) validateEmail: (NSString *) candidate {
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    
    return [emailTest evaluateWithObject:candidate];
}

+ (BOOL) isAllTextFieldNotEmpty:(NSMutableArray *)allTextfields
{
    for (UITextField *tf in allTextfields) {
        if (tf.text.length == 0) {
            [tf becomeFirstResponder];
            return FALSE;
        }
    }
    for (UITextField *tf in allTextfields) {
        [tf resignFirstResponder];
    }
    return TRUE;
}

+ (void)showAnnouncementDialog:(NSString *)message
{
    if (message != nil) {
        dispatch_async(dispatch_get_main_queue(), ^{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:APP_TITLE_LABEL message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        });
    }
}


+ (void) showConfirmationDialog: (NSString *)message dlg: (id)dlg type:(NSInteger)dialogType
{
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle: CONFIRMATION_TITLE_LABEL
                          message:message
                          delegate: dlg
                          cancelButtonTitle:CANCEL_LABEL
                          otherButtonTitles:OK_LABEL, nil];
    alert.tag = dialogType;
    [alert show];
}

//for keychain
+ (KeychainItemWrapper*)getLoginKeychainWrapper
{
    KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:KEYCHAIN_LOGIN_KEYWORD accessGroup:nil];
    return keychainItem;
}

+ (void)resetKeychain
{
    KeychainItemWrapper *item = [self getLoginKeychainWrapper];
    [item resetKeychainItem];
}
+ (void) saveLoginInformationToKeychain:(NSString*)_id password:(NSString*)token
{
    KeychainItemWrapper *item = [self getLoginKeychainWrapper];
    [item setObject:_id forKey:(__bridge id)(kSecAttrAccount)];
    [item setObject:token forKey:(__bridge id)(kSecValueData)];
}

+ (NSMutableArray*) getLoginInformationObjectFromKeychain;
{
    KeychainItemWrapper *item = [self getLoginKeychainWrapper];
    NSString *token = [item objectForKey:(__bridge id)(kSecValueData)];
    NSString *_id = [item objectForKey:(__bridge id)(kSecAttrAccount)];
    NSMutableArray *rs = [[NSMutableArray alloc] init];
    [rs addObject:_id];
    [rs addObject:token];
    return rs;
}

+ (User *)parseUserObject:(NSDictionary *)dict
{
    User *user = [[User alloc] init];
    user.email = (NSString*)dict[@"email"];
    user.lastName = (NSString*)dict[@"last_name"];
    user.id = (NSString*)dict[@"id"];
    user.firstName = (NSString*)dict[@"first_name"];
    user.token = (NSString*)dict[@"token"];
    return  user;
}

@end
